package com.wisezone.permission.service.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.wisezone.common.exception.WzServiceException;
import com.wisezone.common.exception.WzWebException;
import com.wisezone.common.service.impl.BaseServiceImpl;
import com.wisezone.permission.bean.entity.WzDepartmentEntity;
import com.wisezone.permission.bean.form.WzDepartmentForm;
import com.wisezone.permission.bean.search.WzDepartmentSearch;
import com.wisezone.permission.dao.WzDepartmentDao;
import com.wisezone.permission.service.WzDepartmentService;
/**
 *
 *部门; InnoDB free: 24576 kB
 *@author create by 
 *create date 2010-07-29 14:44:40 update date 2010-07-29 14:44:40
 *
 */
public class WzDepartmentServiceImpl extends BaseServiceImpl implements WzDepartmentService{

	 /**
	  *
	  *
	  *
	  */
	 private WzDepartmentDao wzDepartmentDao;
	 

	 /**
	  * 获得部门列表
	  * @param name 部门名称
	  * @return
	  * @throws WzServiceException
	  */
	 public List<WzDepartmentForm> getDepartmentList(String name) throws WzServiceException{
		 
		 WzDepartmentSearch searchEntity = new WzDepartmentSearch();
		 
		 //获得一级部门
		 searchEntity.setParentId(0L);
		 searchEntity.setName(name);
		 searchEntity.setEnableLike(true);
		 List<WzDepartmentForm> firstList = this.wzDepartmentDao.listFormAll(searchEntity);
		 
		 //获得非一级部门
		 searchEntity = new WzDepartmentSearch();
		 searchEntity.setName(name);
		 searchEntity.setEnableLike(true);
		 searchEntity.setSqlClause("t1.parent_id > 0");
		 List<WzDepartmentForm> secondList = this.wzDepartmentDao.listFormAll(searchEntity);
		 
		 List<WzDepartmentForm> list = new ArrayList<WzDepartmentForm>();
		 if(firstList == null || firstList.size() == 0){
			 return list;
		 }
		 for(Iterator<WzDepartmentForm> it = firstList.iterator(); it.hasNext();){
			 WzDepartmentForm entity = it.next();
			 list.add(entity);
			 if(secondList == null || secondList.size() == 0){
			   continue;
			 }
			 for(Iterator<WzDepartmentForm> childIt = secondList.iterator(); childIt.hasNext();){
				 WzDepartmentForm childEntity = childIt.next();
				 if(childEntity.getParent() != null && childEntity.getParent().getId().equals(entity.getId())){
					 entity.addChild(childEntity);
				 }
			 }
		 }
		 return list;
	 }
	 
	 /**
	  * 获得父部门
	  * @return
	  * @throws WzWebException
	  */
	 public List<WzDepartmentForm> getParentList() throws WzWebException{
		 
		 WzDepartmentSearch searchEntity = new WzDepartmentSearch();
		 searchEntity.addSqlClause("parent_id < 1");
		 List<WzDepartmentForm> list = this.listAll(searchEntity);
		 return list;
	 }

	 
	 
	 public void setWzDepartmentDao(WzDepartmentDao wzDepartmentDao){
		 this.setBaseDao(wzDepartmentDao);
		 this.wzDepartmentDao = wzDepartmentDao;
	 }



}